package com.konne.nightmare.FastPublicOpinion.ui.information.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpFragment;
import com.konne.nightmare.FastPublicOpinion.bean.MembersBean;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.SetTeamLeaderPresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.view.ITeamLeaderView;
import com.konne.nightmare.FastPublicOpinion.utils.GlideUtils;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;

import java.util.List;

import butterknife.BindView;

/**
 * Create by ws.
 * on Data:2022/1/26  Time:下午 4:35.
 * description: ** .
 */
public class SetTeamLeaderFragment extends BaseMvpFragment<ITeamLeaderView, SetTeamLeaderPresenter> implements ITeamLeaderView{
    @BindView(R.id.stv_manager)
    SuperTextView stv_manager;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stv_sub_title)
    SuperTextView stv_sub_title;
    private LeaderAdapter mAdapter;
    public static SetTeamLeaderFragment getInstance(){
        return new SetTeamLeaderFragment();
    }
    @Override
    protected SetTeamLeaderPresenter createPresenter() {
        return new SetTeamLeaderPresenter();
    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return null;
    }

    @Override
    public void cancelLoadDialog() {

    }

    @Override
    public int getLayout() {
        return R.layout.frag_student_list_by_group;
    }

    @Override
    public void initView() {
        stv_manager.setText("确定");
        stv_manager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mAdapter.selData==null){
                    ToastUtils.showShort("请选择需要设置组长！");
                    return;
                }
                mPresenter.setTeamLeader(mAdapter.selData);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    }

    @Override
    public void finishAct() {
        getActivity().finish();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mPresenter.init();
    }
    @Override
    public void updataStudent(List<MembersBean> datas) {
        mAdapter=new LeaderAdapter(datas);
        recyclerView.setAdapter(mAdapter);
        stv_sub_title.setText(datas.size() + "人");
    }
    class LeaderAdapter extends BaseQuickAdapter<MembersBean, BaseViewHolder>{
        MembersBean selData;
        public LeaderAdapter(@Nullable List<MembersBean> data) {
            super(R.layout.item_set_teamleader, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, MembersBean item) {
            helper.setText(R.id.tv_name,item.getName())
                    .setVisible(R.id.stv_zz,selData==item)
                    .setVisible(R.id.ic_sel,selData==item);
            ImageView iv_avatar=helper.getView(R.id.iv_avatar);
            GlideUtils.loadCircleImage(item.getAvatar(),iv_avatar);
            helper.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(selData==item){
                        selData=null;
                    }else {
                        selData=item;
                    }
                    notifyDataSetChanged();
                }
            });
        }
    }
}
